﻿using System.Linq;
using System.Web;
using System.Xml.Linq;
using ShopB2C.Interfaces;

namespace ShopB2C.Helper {
    /// <summary>
    /// 数据库访问操作类
    /// </summary>
    public class DataBaseExecuter {
        /// <summary>
        /// 根据传入的数据库访问类名创建类实例
        /// </summary>
        /// <typeparam name="T">类的类型（接口，例：IClassModule）</typeparam>
        /// <param name="className">类名（例：ClassModule）</param>
        /// <returns>类的实例</returns>
        public static T CreateClassByName<T>(string className) {
            XDocument xd = XDocument.Load(HttpContext.Current.Server.MapPath("~/Config/DataAccess.config"));
            string assembly = xd.Root.Elements().Where<XElement>(x => {
                return x.Name == "Assembly";
            }).Select<XElement, string>(x => {
                return x.Value;
            }).First();
            string connecitonString = xd.Root.Elements().First<XElement>(x => {
                return x.Name == "ConnectionString";
            }).Value;
            T classIns = new Factory(assembly, connecitonString).CreateClassByName<T>(className);
            return classIns;
        }
    }
}
